if(typeof G == "undefined") {
    var G = {};
}

if(typeof G.checkout == "undefined") {
    G.checkout = {};
}


G.checkout.main = {

    submit: function() {

        if (G.checkout.log.ziti == 1 && !G.checkout.log.extend_1) {
            E.alert("请选择自提点");
            return false;
        }

        G.checkout.args.invoice_content = $("#invoice_content").val();
        G.checkout.args.remark = $("#remark").val();
        E.loadding.open("正在提交订单，请稍候...");

        E.ajax_post({
            action: "bill",
            operFlg: 1,
            data: G.checkout.args,
            call: "G.checkout.main.result"
        });

    },

    result: function( o ) {
        if (o.code == 200) {
            if (G.args.is_platform) {
                self.location = G.args.payment + '?showwxpaytitle=1&id=' + o.data.bill_no + '&emt=0.0.3.5.'+o.data.bill_no+'.0';
            } else {
                self.location = G.args.payment + '?id=' + o.data.bill_no + '&emt=0.0.3.5.'+o.data.bill_no+'.0';
            }
        } else {
            E.loadding.close();
            E.alert(o.message);
        }
    },

    compute: function() {
        $("#coupon_money").text(G.checkout.args.coupon_money.toFixed(2));
        $("#balance_money").text(G.checkout.args.balance.toFixed(2));
        var total = G.checkout.args.total + G.checkout.args.freight - G.checkout.args.coupon_money - G.checkout.args.balance - G.checkout.args.subtractMoney;
        $("#total").text(total.toFixed(2));
    }

};

G.checkout.main.compute();

G.checkout.account = {

    search: function() {
        E.ajax_get({
            url: '/invokingAjax-shop/customer/account.ajax?operFlg=1',
            call: 'G.checkout.account.searchResult'
        });
    },

    searchResult: function( o ) {
        if (!o.data) {
            return false;
        }
        if (o.data.balance > 0) {
            var balance = parseFloat(o.data.balance);
            $('#balance_item').text(balance.toFixed(2));
            $('#balance_box').show();
            G.checkout.args.usableBalance = balance;
        }
        if (o.data.points > 0) {
            var points = parseInt(o.data.points);
            G.checkout.args.usablePoints = points;
        }
    },

    useBalance: function() {

        var balance = E.trim($("#balance").val()),
            usableBalance = G.checkout.args.usableBalance,
            bill_total = G.checkout.args.total + G.checkout.args.freight - G.checkout.args.coupon_money - G.checkout.args.subtractMoney;

        if (balance == "" || balance <= 0) {
            E.alert("请输入账户余额");
            return false;
        }
        if (balance > usableBalance) {
            E.alert("输入账户余额大于可用账户余额");
            return false;
        }
        if (balance > bill_total) {
            E.alert("输入账户余额大于订单应付金额");
            return false;
        }

        E.loadding.open("正在使用账户余额，请稍候...");
        E.ajax_get({
            action: "checkout",
            operFlg: 9,
            data: {
                balance: balance
            },
            call: "G.checkout.account.useBalanceResult"
        });

    },

    useBalanceResult: function( o ) {
        E.loadding.close();
        if (o.code == 200) {
            G.checkout.args.balance = parseFloat(o.data.balance);
            $("#balance").attr("disabled", true);
            $("#balance_btn").html("<a onclick=\"G.checkout.account.cancelUseBalance();\" class=\"btn\">取消使用</a>");
            G.checkout.main.compute();
        } else {
            E.alert(o.message);
        }
    },

    cancelUseBalance: function() {
        E.loadding.open("正在取消使用账户余额，请稍候...");
        E.ajax_get({
            action: "checkout",
            operFlg: 10,
            call: "G.checkout.account.cancelUseBalanceResult"
        });
    },

    cancelUseBalanceResult: function( o ) {
        E.loadding.close();
        if (o.code == 200) {
            G.checkout.args.balance = 0;
            G.checkout.main.compute();
            $("#balance").val("").removeAttr("disabled");
            $("#balance_btn").html("<a onclick=\"G.checkout.account.useBalance();\" class=\"btn\">使用</a>");
        } else {
            E.alert(o.message);
        }
    }

};

G.checkout.coupon = {

    code: "", //已验证的优惠券编号

    //查询优惠券
    search: function( page ) {

        E.ajax_get({
            action: "checkout",
            operFlg: 6,
            data: {
                page: page,
                act: G.checkout.args.act
            },
            call: "G.checkout.coupon.searchResult"
        });

        $("#coupon_box").removeClass("hide");
        $("#coupon_click").addClass("hide");

    },


    //查询已有优惠券返回结果
    searchResult: function( o ) {

        if (o.code == 200) {

            if (o.data.coupon) {

                var html = '<table class="s">';
                html += '<tbody>';
                $.each(o.data.coupon, function(k, v) {
                    html += '<tr>';
                    html += '<td>';
                    html += '<p>名称: ' + v.couponName + '</p>';
                    html += '<p>面值: ' + v.amount + '元';
                    if (G.checkout.coupon.code) {
                        html += '&nbsp;&nbsp;<a href="javascript: void(0);" style="color: #0000EE;" onclick="G.checkout.coupon.select(\'' + v.code + '\');" class="hide">使用</a></p>';
                    } else {
                        html += '&nbsp;&nbsp;<a href="javascript: void(0);" style="color: #0000EE;" onclick="G.checkout.coupon.select(\'' + v.code + '\');">使用</a></p>';
                    }
                    html += '</td>';
                    html += '</tr>';
                });
                html += '</tbody>';
                html += '</table>';
                if (o.data.paging) {
                    html += o.data.paging;
                }
                $("#coupon_list").html(html).removeClass("hide");

            }

        } else {

            E.alert(o.message);

        }

    },

    //选择已有的优惠券
    select: function( code ) {
        $("#couponCode").val(code);
        G.checkout.coupon.use();
    },

    //使用优惠券
    use: function() {

        var code = E.trim($("#couponCode").val());

        if (E.isEmpty(code)) {
            E.alert('请输入优惠券');
        } else {
            E.loadding.open("正在使用优惠券，请稍候...");
            E.ajax_get({
                action: "checkout",
                operFlg: 7,
                data: {
                    code: code,
                    act: G.checkout.args.act
                },
                call: "G.checkout.coupon.useResult"
            });
        }

    },

    //验证优惠券结果
    useResult: function( o ) {

        E.loadding.close();

        if (o.code == 200) {

            G.checkout.args.coupon_money = parseInt(o.data.amount);
            G.checkout.main.compute();
            $("#coupon_list").find("table.s a").addClass("hide");
            $("#couponCode").attr("disabled", true);
            $("#coupon_btn").val("取消使用").attr("onclick", "G.checkout.coupon.cancelUse();");
            this.code = o.data.code;

        } else {
            E.alert(o.message);
        }

    },

    //取消优惠券
    cancelUse: function() {
        E.loadding.open("正在取消使用优惠券，请稍候...");
        E.ajax_get({
            action: "checkout",
            operFlg: 8,
            data: {
                act: G.checkout.args.act
            },
            call: "G.checkout.coupon.cancelResult"
        });
    },

    cancelResult: function( o ) {

        E.loadding.close();

        if (o.code == 200) {

            G.checkout.args.coupon_money = 0;
            G.checkout.main.compute();
            $("#coupon_list").find("table.s a").removeClass("hide");
            $("#couponCode").removeAttr("disabled").val("");
            $("#coupon_btn").val("使用").attr("onclick", "G.checkout.coupon.use();");
            this.code = "";

        } else {
            E.alert(o.message);
        }

    },

    couponBoxShow: function() {
        $('coupon_box').removeClass('hide');
    }

};

G.checkout.coupon.search(1);
G.checkout.account.search();